package com.brillilab.service.core.method.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.brillilab.dao.mapper.method.MethodRecommendMapper;
import com.brillilab.dao.mapper.method.MethodRecommendReceiveMapper;
import com.brillilab.domain.enums.common.BoolEnum;
import com.brillilab.domain.po.method.MethodRecommend;
import com.brillilab.domain.po.method.MethodRecommendReceive;
import com.brillilab.service.core.method.IMethodRecommendService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author wuzhiyu
 * @Title: MethodRecommendServiceImpl
 * @ProjectName brill_lab
 * @Description: TODO
 * @date 2019/2/2313:17
 */
@Service
@Transactional
public class MethodRecommendServiceImpl implements IMethodRecommendService {
    @Resource
    private MethodRecommendMapper methodRecommendMapper;
    @Resource
    private MethodRecommendReceiveMapper methodRecommendReceiveMapper;

    @Override
    public boolean saveReceive(MethodRecommendReceive receive) {
        return methodRecommendReceiveMapper.insert(receive)>0;
    }

    @Override
    public MethodRecommend getRecommend(Long recommendId, Long labId) {
        LambdaQueryWrapper<MethodRecommend> query = new LambdaQueryWrapper<>();
        query.eq(MethodRecommend::getId, recommendId);
        query.eq(MethodRecommend::getLabId, labId);
        return methodRecommendMapper.selectOne(query);
    }

    @Override
    public boolean updateById(MethodRecommend recommend) {
        return methodRecommendMapper.updateById(recommend) > 0;
    }

    @Override
    public MethodRecommendReceive getReceiveByRecommendIdAndMemberId(Long recommendId, Long labMemberId) {
        LambdaQueryWrapper<MethodRecommendReceive> query = new LambdaQueryWrapper<>();
        query.eq(MethodRecommendReceive::getMethodRecommendId,recommendId);
        query.eq(MethodRecommendReceive::getLabMemberId,labMemberId);
        query.orderByDesc(MethodRecommendReceive::getState);
        List<MethodRecommendReceive> list= methodRecommendReceiveMapper.selectList(query);
        return list.size()>0?list.get(0):null;
    }

    @Override
    public MethodRecommend getRecommendByLabIdAndMethodId(Long labId,Long methodId) {
        LambdaQueryWrapper<MethodRecommend> query = new LambdaQueryWrapper<>();
        query.eq(MethodRecommend::getLabId,labId);
        query.eq(MethodRecommend::getMethodId,methodId);
        return methodRecommendMapper.selectOne(query);
    }

    @Override
	public MethodRecommend addRecommend(MethodRecommend recommend) {
		methodRecommendMapper.insert(recommend);
		return recommend;
	}


    @Override
    public List<MethodRecommend> getRecommendList(List<Long> recommendIds,Long labId) {
        LambdaQueryWrapper<MethodRecommend> query = new LambdaQueryWrapper<>();
        query.in(MethodRecommend::getId, recommendIds);
        query.eq(MethodRecommend::getLabId, labId);
        query.eq(MethodRecommend::getState, BoolEnum.TRUE.getValue());
        return methodRecommendMapper.selectList(query);
    }

    @Override
    public int saveReceiveBatch(List<MethodRecommendReceive> receiveList) {
        return methodRecommendReceiveMapper.saveBatch(receiveList);
    }

    @Override
    public List<MethodRecommend> selectListByLabId(Long labId) {
        return methodRecommendMapper.selectList(
                new LambdaQueryWrapper<MethodRecommend>()
                        .eq(MethodRecommend::getLabId,labId)
                        .eq(MethodRecommend::getState, BoolEnum.TRUE.getValue()));
    }

	@Override
	public boolean update(Long methodId, Long labId, Integer state) {
		 LambdaQueryWrapper<MethodRecommend> query = new LambdaQueryWrapper<>();
	     query.in(MethodRecommend::getMethodId, methodId);
	     query.eq(MethodRecommend::getLabId, labId);
	     
	     MethodRecommend entity = new MethodRecommend();
	     entity.setMethodId(methodId);
	     entity.setLabId(labId);
	     entity.setState(state);
		return methodRecommendMapper.update(entity, query)>0;
	}
}
